/*
 * @Author: lwHao
 * @Date: 2020-07-17 11:04:33
 * @LastEditors: lwHao
 * @LastEditTime: 2020-07-17 17:40:57
 */
// 'use strict'
console.log(this);

class Node {
  constructor(next) {
    this.value = 1
    this.next = next
  }
  isOut() {
    return !this.value
  }
  out() {
    this.value = 0
  }
}

// 获胜人数
const winerCount = 1
// 参与人数
const participantCount = 40
// 链表头
let headPoint = null
// 每次循环后记录上一个节点
let prevNode = null
for (let i = 0; i < participantCount; i++) {
  
  let currentNode = new Node(null)
  if(prevNode){
    prevNode.next = currentNode
  }else{
    // 确定链头
    headPoint = currentNode
  }
  prevNode = currentNode
}
// 链尾指向链头
prevNode.next = headPoint

// 当前所数到的数
let count = 0
// 已出局的人数
let outCount = 0

let currentNode = headPoint
while (true) {
  // break
  // let currentNode = circleList[index]
  if (!currentNode.isOut()) {
    count++
  }
  if (count === 3) {
    // 出局一个
    currentNode.out()
    // 归零数数
    count = 0
    // 出局人数加一
    outCount++
  }
  // 出局人数等于38结束
  if (outCount === participantCount - winerCount) {
    console.log('game over');
    break;
  }
  currentNode = currentNode.next
}

let point = headPoint
console.log('circle length:', point);
for(let i = 0; i < participantCount; i++){
  if(!point.isOut()){
    console.log('win position:', i)
  }
  point = point.next
}

// for(let i = 0 ;i < circleList.length; i++){
//   if(!circleList[i].isOut()){
//     console.log('win position:', i);
//   }
// }